<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pipeline</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        h1 {
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
            position: sticky;
            top: 0;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        .loading {
            text-align: center;
            padding: 20px;
            font-style: italic;
            color: #666;
        }

        .error {
            color: red;
            font-weight: bold;
        }

        .exec-btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 6px 12px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }

        .exec-btn:hover {
            background-color: #45a049;
        }

        .exec-btn:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }

        .status {
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
<h1>Pipeline 列表</h1>
<div id="loading" class="loading">加载中...</div>
<div id="error" class="error" style="display: none;"></div>
<div id="content" style="display: none;">
    <table id="pipelineTable">
        <thead>
        <tr>
            <th>ID</th>
            <th>主机名</th>
            <th>Pipeline名称</th>
            <th>类型</th>
            <th>Git仓库</th>
            <th>分支</th>
            <th>Docker镜像</th>
            <th>运行命令</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="pipelineTableBody">
        <!-- 数据将通过JavaScript动态填充 -->
        </tbody>
    </table>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        fetchPipelineData();
    });

    function fetchPipelineData() {
        fetch('/api/pipeline')
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应不正常');
                }
                return response.json();
            })
            .then(data => {
                document.getElementById('loading').style.display = 'none';
                document.getElementById('content').style.display = 'block';
                populateTable(data);
            })
            .catch(error => {
                console.error('获取数据时出错:', error);
                document.getElementById('loading').style.display = 'none';
                document.getElementById('error').style.display = 'block';
                document.getElementById('error').textContent = '加载数据失败: ' + error.message;
            });
    }

    function populateTable(pipelines) {
        const tableBody = document.getElementById('pipelineTableBody');

        pipelines.forEach(pipeline => {
            const row = document.createElement('tr');

            // 格式化日期时间
            const createdAt = new Date(pipeline.CreatedAt).toLocaleString();
            const updatedAt = new Date(pipeline.UpdatedAt).toLocaleString();

            // 创建表格单元格
            row.innerHTML = `
                    <td>${pipeline.ID}</td>
                    <td>${pipeline.hostname}</td>
                    <td>${pipeline.pipelineName}</td>
                    <td>${pipeline.pipelineType}</td>
                    <td>${pipeline.gitRepoUrl}</td>
                    <td>${pipeline.branchName}</td>
                    <td>${pipeline.dockerImageName}:${pipeline.dockerImageTag}</td>
                    <td>${pipeline.dockerRunCommand}</td>
                    <td>${createdAt}</td>
                    <td>${updatedAt}</td>
                    <td>
                        <button class="exec-btn" onclick="execPipeline(${pipeline.ID}, this)">执行</button>
                        <span class="status" id="status-${pipeline.ID}"></span>
                    </td>
                `;

            tableBody.appendChild(row);
        });
    }

    function execPipeline(id, button) {
        const statusElement = document.getElementById(`status-${id}`);

        // 禁用按钮并显示加载状态
        button.disabled = true;
        statusElement.textContent = "执行中...";
        statusElement.style.color = "#2196F3";

        fetch(`/api/pipeline/exec/${id}`, {
            method: 'POST'
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('执行失败');
                }
                return response.json();
            })
            .then(data => {
                statusElement.textContent = "执行成功";
                statusElement.style.color = "#4CAF50";
                // 3秒后清除状态
                setTimeout(() => {
                    statusElement.textContent = "";
                }, 3000);
            })
            .catch(error => {
                console.error('执行出错:', error);
                statusElement.textContent = "执行失败: " + error.message;
                statusElement.style.color = "#f44336";
            })
            .finally(() => {
                // 3秒后重新启用按钮
                setTimeout(() => {
                    button.disabled = false;
                }, 3000);
            });
    }
</script>
</body>
</html>